package cn.wolfcode.luowowo.member.service.impl;

import cn.wolfcode.luowowo.cache.service.IUserInfoRedisService;
import cn.wolfcode.luowowo.common.exception.LogicException;
import cn.wolfcode.luowowo.common.util.AssertUtil;
import cn.wolfcode.luowowo.common.util.Consts;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import cn.wolfcode.luowowo.member.mapper.UserInfoMapper;
import cn.wolfcode.luowowo.member.service.IUserInfoService;
import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Service;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;

import java.util.List;
import java.util.UUID;

@Service    //dubbo里面的服务标签

public class UserInfoServiceImpl implements IUserInfoService {
    @Autowired  //组件与组件之间调用
    private UserInfoMapper userInfoMapper;
    @Reference  //服务与服务之间调用
    private IUserInfoRedisService userInfoRedisService;

    @Override
    public UserInfo get(long id) {
        return userInfoMapper.selectByPrimaryKey(id);
    }

    @Override
    public boolean checkPhone(String phone) {
        return userInfoMapper.selectCountByPhone(phone)>0;
    }

    @Override
    public void sendVerifyCode(String phone) {

        //创建手机验证码
        String code = UUID.randomUUID().toString().substring(0, 4);

        //发送到redis缓存中
        userInfoRedisService.setVerifyCode(phone,code);//调用cache-api中的接口,传递参数包括phone,code

        //发送手机验证码
        StringBuilder sb = new StringBuilder(100);
        //调用工具类consts中的时延字段作为限时,并使用append进行拼接,显示给用户看
        sb.append("您的验证码是").append(code).append(",请在").append(Consts.VERIFY_CODE_VAI_TIME).append("分钟内使用");
        //假装已经发送了验证码
        System.out.println(sb.toString());
    }

    //用户注册功能
    @Override
    public void userRegist(String phone, String nickname, String password, String rpassword, String verifyCode) {
        //必须校验参数是否为空
        AssertUtil.hasLength(phone,"号码不能为空");
        AssertUtil.hasLength(nickname,"昵称不能为空");
        AssertUtil.hasLength(password,"密码不能为空");
        AssertUtil.hasLength(rpassword,"确认密码不能为空");
        AssertUtil.hasLength(verifyCode,"验证码不能为空");
        //必须校验手机号码是否存在
        if (userInfoMapper.selectCountByPhone(phone)>0){
            throw new LogicException("手机号已存在");
        }
        //必须校验两次输入的密码是否相等
        AssertUtil.isEquals(password,rpassword,"两次输入的密码不相等");
        //必须校验短信验证码是否正确
        String verifyCode1 = userInfoRedisService.getVerifyCode(phone);
        if (!verifyCode.equalsIgnoreCase(verifyCode1)){
            throw new LogicException("验证码不正确");
        }
        //加密
        password = phone + password;
        String md5Password = DigestUtils.md5DigestAsHex(password.getBytes());
        //注册
        UserInfo userInfo = new UserInfo();
        userInfo.setNickname(nickname);
        userInfo.setPhone(phone);
        userInfo.setPassword(md5Password);
        userInfo.setLevel(1);
        userInfo.setState(userInfo.STATE_NORMAL);
        userInfo.setHeadImgUrl("/images/default.jpg");
        userInfoMapper.insert(userInfo);
    }

    //用户登陆功能
    @Override
    public String userLogin(String username, String password) {
        UserInfo userInfo = userInfoMapper.selectUserByUsernameAndPassword(username,password);
        //判断是否有数据
        if (userInfo==null){
            throw new LogicException("账号或者密码错误");
        }
        //如果有数据,使用uuid
        String token = UUID.randomUUID().toString().replace("-", ".");
        //将token的值作为key存在redis中
        userInfoRedisService.addToken(token,userInfo);
        return token;
    }

    @Override
    public List<UserInfo> list() {
        return userInfoMapper.selectAll();
    }
}
